#include <random> 
#include <google/protobuf/map.h>

class Utils
{
public:
    static unsigned int random_char() {
        static std::random_device rd;
        static std::mt19937 gen(rd());
        std::uniform_int_distribution<> dis(0, 255);
        return dis(gen);
    }

    // 生成 UUID （8-4-4-4-12 格式）
    static std::string generate_uuid() {
        const int uuid_length = 16; // 16 字节
        unsigned char uuid_bytes[uuid_length];

        for (int i = 0; i < uuid_length; i++) {
            uuid_bytes[i] = static_cast<unsigned char>(random_char());
        }

        // 设置 UUID 版本号（4 表示随机 UUID）
        uuid_bytes[6] = (uuid_bytes[6] & 0x0F) | 0x40;
        // 设置 UUID variant（10xxxxxx）
        uuid_bytes[8] = (uuid_bytes[8] & 0x3F) | 0x80;

        std::stringstream ss;
        ss << std::hex << std::nouppercase;

        for (int i = 0; i < uuid_length; i++) {
            ss.width(2);
            ss.fill('0');
            ss << static_cast<int>(uuid_bytes[i]);

            // 插入分隔符
            if (i == 3 || i == 5 || i == 7 || i == 9) {
                ss << "-";
            }
        }

        return ss.str();
    }

    // map 拷贝
    static void map_copy(google::protobuf::Map<std::string, std::string>* target, 
                         const google::protobuf::Map<std::string, std::string>& source) {
        if (nullptr == target) {
            std::cout << "map_copy warning, target is nullptr!" << std::endl;
            return;
        }
        for (auto it = source.cbegin(); it != source.cend(); ++it) {
            target->insert({it->first, it->second});
        }
    }
};
